/**
 * @file 获取设备、浏览器、操作系统相关信息
 */

/**
 * 判断是否为 IE
 */
export const isIE = () => !!window.ActiveXObject || 'ActiveXObject' in window

/**
 * 判断是否为 IE 11
 */
export const isIE11 = () => navigator.userAgent.toLowerCase().match(/rv:([\d.]+)\) like gecko/)

/**
 * 判断是否为 Edge
 */
export const isEdge = () => /Edge/.test(navigator.userAgent)

/**
 * 判断是否为 Firefox
 */
export const isFirefox = () => /Firefox/.test(navigator.userAgent)

/**
 * 判断是否为 Opera
 */
export const isOpera = () => /Opera/.test(navigator.userAgent)

/**
 * 判断是否为 Safari
 */
export const isSafari = () =>
  /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)

/**
 * 判断是否为 Chrome
 */
export const isChrome = () => /Chrome/.test(navigator.userAgent)

/**
 * 判断是否为 移动端
 */
export const isMobile = () =>
  navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )

/**
 * 判断是否为 PC
 */
export const isPC = () => !isMobile()

/**
 * 判断是否为 Windows
 */
export const isWindows = () => /win/i.test(navigator.appVersion.toLowerCase())

/**
 * 判断是否为 Mac
 */
export const isMac = () => /mac/i.test(navigator.appVersion.toLowerCase())

/**
 * 判断是否为 Linux
 */
export const isLinux = () => /linux/i.test(navigator.appVersion.toLowerCase())

/**
 * 判断是否为 安卓
 */
export const isAndroid = () =>
  navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1

/**
 * 判断是否为 华为
 */
export const isHuawei = () => navigator.userAgent.toLocaleLowerCase().indexOf('huawei') > -1

/**
 * 判断是否为 IOS
 */
export const isIOS = () => !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)

/**
 * 判断是否为 微信
 */
export const isWx = () => navigator.userAgent.match(/MicroMessenger/gi)

/**
 * 判断是否为 QQ
 */
export const isQQ = () => {
  const ua = navigator.userAgent.toLowerCase()
  if (isIOS()) {
    return ua.indexOf(' qq') > -1 && ua.indexOf('mqqbrowser') < 0
  } else if (isAndroid()) {
    return ua.indexOf(' qq') > -1 && ua.indexOf('mqqbrowser') > -1
  }
  return false
}

/**
 * 判断是否为 阿里
 */
export const isAli = () => navigator.userAgent.match(/AlipayClient/gi)
